/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.Ymkdz;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.domain.sup.Rygl;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.HljgService;
import me.zhengjie.business.service.RyglService;
import me.zhengjie.business.service.YmkdzService;
import me.zhengjie.business.service.dto.HljgKsQueryCriteria;
import me.zhengjie.business.service.dto.YmkdzQueryCriteria;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2022-04-15
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "异名库对照管理")
@RequestMapping("/api/ymkdz")
public class YmkdzController {

    private final YmkdzService ymkdzService;

    private final HljgService hljgService;

    private final RyglService ryglService;

    private final GsglService gsglService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ymkdz:list')")
    public void exportYmkdz(HttpServletResponse response, YmkdzQueryCriteria criteria) throws IOException {
        ymkdzService.download(ymkdzService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询异名库对照")
    @PreAuthorize("@el.check('ymkdz:list')")
    public ResponseEntity<Object> queryYmkdz(YmkdzQueryCriteria criteria, Pageable pageable){
      /*  Hljg hljg = new Hljg();
        hljg.setBisHljgId(SecurityUtils.getCurrentUserId());
        criteria.setHljg(hljg);*/
        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);
        return new ResponseEntity<>(ymkdzService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/queryAllHljgYmk")
    @ApiOperation("查询所有护理机构服务项目")
    public ResponseEntity<Object> queryAllHljgYmk(YmkdzQueryCriteria criteria){

        Long userid = SecurityUtils.getCurrentUserId();
        Gsgl gsgl = gsglService.queryGsgl(userid);
        Hljg hljg = new Hljg();
        hljg.setBisGsglId(gsgl.getBisGsglId());

        criteria.setHljg(hljg);

        return new ResponseEntity<>(ymkdzService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping(value = "/queryHljgFwxm")
    @ApiOperation("移动端查询服务项目")
    public ResponseEntity<Object> queryHljgFwxm(YmkdzQueryCriteria criteria) throws IOException {

        return new ResponseEntity<>(ymkdzService.queryFwxm(criteria),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增异名库对照")
    @ApiOperation("新增异名库对照")
    @PreAuthorize("@el.check('ymkdz:add')")
    public ResponseEntity<Object> createYmkdz(@Validated @RequestBody Ymkdz resources){
        return new ResponseEntity<>(ymkdzService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改异名库对照")
    @ApiOperation("修改异名库对照")
    @PreAuthorize("@el.check('ymkdz:edit')")
    public ResponseEntity<Object> updateYmkdz(@Validated @RequestBody Ymkdz resources){
        ymkdzService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除异名库对照")
    @ApiOperation("删除异名库对照")
    @PreAuthorize("@el.check('ymkdz:del')")
    public ResponseEntity<Object> deleteYmkdz(@RequestBody String[] ids) {
        ymkdzService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}